package com.zxbit.flash.ui.fragment;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.zxbit.flash.BuildConfig;
import com.zxbit.flash.R;
import com.zxbit.flash.base.BaseFragment;
import com.zxbit.flash.common.Constant;
import com.zxbit.flash.common.RouterMap;
import com.zxbit.flash.common.UserInfo;
import com.zxbit.flash.mvpview.PersonalInfoMvpView;
import com.zxbit.flash.network.Urls;
import com.zxbit.flash.presenter.PersonalInfoPresenter;
import com.zxbit.flash.response.PersonalInfoResponse;
import com.zxbit.flash.rxbus.EventMsg;
import com.zxbit.flash.rxbus.Events;
import com.zxbit.flash.rxbus.RxBus;
import com.zxbit.flash.utils.MaskUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class MineFragment extends BaseFragment implements PersonalInfoMvpView {

    @BindView(R.id.tv_username)
    TextView tvUserName;
    @BindView(R.id.tv_login_out)
    TextView tvLoginOut;
    private PersonalInfoPresenter presenter;

    @Override
    public int getContentView() {
        return R.layout.fragment_mine;
    }

    @SuppressLint("CheckResult")
    @Override
    public void onFragmentCreated(View rootView) {
        ButterKnife.bind(this, rootView);
        presenter = new PersonalInfoPresenter(this);
        RxBus.getInstance().toObservable().map(new Function<Object, EventMsg>() {
            @Override
            public EventMsg apply(Object o) throws Exception {
                return (EventMsg) o;
            }
        }).subscribe(new Consumer<EventMsg>() {
            @Override
            public void accept(EventMsg eventMsg) throws Exception {
                if (eventMsg != null) {
                    if (eventMsg.getMsg().equals(Events.LOGIN_SUCCESS)) {
                        getPersonalInfo();
                    } else if (eventMsg.getMsg().equals(Events.LOGOUT)) {
                        tvUserName.setText("");
                    }
                }
            }
        });
        if (BuildConfig.DEBUG) {
            tvLoginOut.setVisibility(View.VISIBLE);
        } else {
            tvLoginOut.setVisibility(View.GONE);
        }
        getPersonalInfo();
    }

    @OnClick({R.id.tv_bill, R.id.tv_bank_card, R.id.tv_help_center, R.id.tv_about_us, R.id.tv_login_out})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_bill:
                //我的账单
                ARouter.getInstance().build(RouterMap.MINE_BROWING).navigation();
                break;
            case R.id.tv_bank_card:
                //我的银行卡
                ARouter.getInstance().build(RouterMap.MINE_BANK_CARD).navigation();
                break;
            case R.id.tv_help_center:
                //帮助中心
                ARouter.getInstance().build(RouterMap.HELPCENTER_PAGE).navigation();
                break;
            case R.id.tv_about_us:
                //关于我们
                ARouter.getInstance().build(RouterMap.WEB_VIEW_PAGE)
                        .withString(Constant.PARAMS_URL, Urls.ABOUT_US_URL).withInt(Constant.PARAMS_TYPE,Constant.WEB_TYPE_ABOUT).navigation();
                break;
            case R.id.tv_login_out:
                UserInfo.logout();
                EventMsg msg = new EventMsg();
                msg.setMsg(Events.LOGOUT);
                RxBus.getInstance().post(msg);
                break;
            default:
                break;
        }
    }

    private void getPersonalInfo() {
        if (UserInfo.isLogin()) {
//            presenter.getAuthStatus();
            presenter.getPersonalData();
        }
    }

    @Override
    public void onGetDataNext(PersonalInfoResponse response) {
        if (response.getStatus() == Constant.HTTP_OK) {
            MaskUtils.setText(tvUserName, response.getData().getUserName());
        }
    }

//    @Override
//    public void onGetDataNext(AuthResponse response) {
//        Acache.get(getActivity()).put(AcacheKeys.AUTH, new Gson().toJson(response.getData(),
//                AuthData.class));
//        handleAuthStatus(response);
//    }
//
//    private void handleAuthStatus(AuthResponse response) {
//        if (Constant.AUTH_YES == response.getData().getStatus()) {
//            presenter.getPersonInfoBack();
//        }
//    }
//
//    @Override
//    public void onGetDataNext(UserInformationResponse response) {
//        final UserInformationData data = response.getData();
//        if (data.getIsInformationWrong() == Constant.USER_DATA_BACK) {
//            if (!StringUtils.isEmpty(data.getAuthPageCode())) {
//                Acache.get(PopApplication.getContext()).put(AcacheKeys.BACK, data.getUserUuid());
//            }
//        } else {
//            Acache.get(PopApplication.getContext()).put(AcacheKeys.BACK, "");
//        }
//    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }

    @Override
    public void onFragmentDestoryed() {}
}
